import {
  cancelRegister,
  deleteEvent,
  editEvent,
  publishEvent,
  registerEvent,
  searchEvent,
  searchEventById,
  searchJoinUser,
  updateEventStatus,
  searchEventByClubId
} from '@/service/event.js'
import { defineStore } from 'pinia'
const useEventStore = defineStore('event', {
  state: () => ({
    eventList: [],
    eventTotalCount: 0,
    eventMemberList: [],
    event: {}
  }),
  actions: {
    // 发布活动
    async publishEventAction(eventInfo) {
      await publishEvent(eventInfo)
    },
    // 编辑活动
    async editEventAction(event_id, eventInfo) {
      await editEvent(event_id, eventInfo)
    },
    // 删除活动
    async deleteEventAction(event_id, eventInfo) {
      await deleteEvent(event_id)
      await this.getEventAction(eventInfo)
    },
    // 搜索活动
    async getEventAction(queryInfo) {
			console.log(queryInfo,123);
      const res = await searchEvent(queryInfo)
      this.eventList = res.data.list
      this.eventTotalCount = res.data.totalCount
    },
    // 更新活动状态
    async updateEventStatusAction(
      event_id,
      status,
      eventInfo
    ) {
      await updateEventStatus(event_id, status)
      await this.getEventAction(eventInfo)
    },
    // 根据id查询活动
    async searchEventByIdAction(event_id) {
      const res = await searchEventById(event_id)
      this.event = res.data[0]
    },
    // 根据id查询参与的人
    async searchJoinUserAction(event_id) {
      const res = await searchJoinUser(event_id)
      this.eventMemberList = res.data
    },
		
		// 参加报名
	async registerEventAction(event_id, user_id) {
		const res = await registerEvent(event_id, user_id)
		this.searchJoinUserAction(event_id)
		this.searchEventByIdAction(event_id)
	},
		
	
    // 取消报名
    async cancelRegisterAction(event_id, user_id) {
      await cancelRegister(event_id, user_id)
      this.searchJoinUserAction(event_id)
			this.searchEventByIdAction(event_id)
    },
	
	// 根据社团id获取活动
	async searchEventByClubIdAction(club_id) {
		const res = await searchEventByClubId(club_id)
		this.eventList = res.data
	}
  }
})

export default useEventStore
